package com.itqdkj.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itqdkj.health.dao.OrderSettingDao;
import com.itqdkj.health.pojo.OrderSetting;
import com.itqdkj.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{
    @Autowired
    OrderSettingDao orderSettingDao;

    /**
     * 批量添加预约数据
     * @param list
     */
    @Override
    public void add(List<OrderSetting> list) {
         if(list !=null && list.size()>0){
             for (OrderSetting orderSetting : list) {
                 //检查此数据（日期）是否存在
                 long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                  if(count > 0){
                      //已经存在，执行更新操作
                      orderSettingDao.editNumberByOrderDate(orderSetting);
                  }else {
                      //不存在，执行添加操作
                      orderSettingDao.add(orderSetting);
                  }
             }
         }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //1.组织查询Map，dateBegin表示月份开始时间，dateEnd月份结束时间
        String dateBegin = date + "-1";//2019-03-1
        String dateEnd = date + "-31";//2019-03-31
        /*Map<String,Object> map = new HashMap();
        map.put("deteBegin",dateBegin);
        map.put("dateEnd",dateEnd);*/
        //2.查询当前月份的预约设置
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(dateBegin,dateEnd);
        //3.将List<OrderSetting>，转换成List<Map>
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期
            orderSettingMap.put("number",orderSetting.getNumber());//可获得预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void setOrderSetting(OrderSetting orderSetting) {
        //使用预约设置的时间，查询预约设置表，判断数据是否存在
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        //有数据，更新
         if(count>0){
            //根据预约设置的时间，更新最多预约的人数字段
            orderSettingDao.editNumberByOrderDate(orderSetting);
         }else {
             //没有数据，新增
             orderSettingDao.add(orderSetting);
         }
    }
}
